package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.StbCompany;
import com.ruoyi.system.domain.StbIntellectualProperty;
import com.ruoyi.system.mapper.StbCompanyMapper;
import com.ruoyi.system.mapper.StbIntellectualPropertyMapper;
import com.ruoyi.system.service.IStbIntellectualPropertyService;

/**
 * 知识产权Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-02-08
 */
@Service
public class StbIntellectualPropertyServiceImpl implements IStbIntellectualPropertyService 
{
    @Autowired
    private StbIntellectualPropertyMapper stbIntellectualPropertyMapper;
    @Autowired
    private StbCompanyMapper stbCompanyMapper;
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    /**
     * 导入知识产权数据
     * 
     * @param propertyList 知识产权数据
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importProperty(List<StbIntellectualProperty> propertyList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(propertyList) || propertyList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (StbIntellectualProperty property : propertyList)
        {
        	String name="";
        	if(StringUtils.isNotEmpty(property.getCompName())) {
            	name+=""+property.getCompName()+"的";
            }
        	 if(StringUtils.isNotEmpty(property.getIproName())) {
             	name+=property.getIproName();
             }
            if(StringUtils.isNotEmpty(property.getIproCode())) {
            	name+="("+property.getIproCode()+")";
            }
            try
            {
            	//查询企业id
            	StbCompany stbCompany = new StbCompany();
            	stbCompany.setCompName(property.getCompName());
            	List<StbCompany> companyList = stbCompanyMapper.selectStbCompanyList(stbCompany);
            	if(companyList!=null&&companyList.size()>0&&StringUtils.isNotEmpty(property.getCompName())) {
            		StbCompany company = companyList.get(0);
            		property.setCompId(company.getCompId());
            		StbIntellectualProperty currentProperty = new StbIntellectualProperty();
            		currentProperty.setCompId(company.getCompId());
                	currentProperty.setIproName(property.getIproName());
                	currentProperty.setIproCode(property.getIproCode());
                    // 验证是否存在这个用户
                	List<StbIntellectualProperty> currentPropertyList = stbIntellectualPropertyMapper.selectStbIntellectualPropertyList(currentProperty);
	            	
                	if(StringUtils.isNotEmpty(property.getIproName())) {
                		if (currentPropertyList==null||currentPropertyList.size()<=0)
                        {
                            property.setCreateBy(operName);
                            this.insertStbIntellectualProperty(property);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、" + name + "知识产权  导入成功");
                        }
                        else if (isUpdateSupport&&currentPropertyList.size()>0)
                        {
                        	StbIntellectualProperty oldProperty = currentPropertyList.get(0);
                        	property.setIproId(oldProperty.getIproId());
                            property.setUpdateBy(operName);
                            this.updateStbIntellectualProperty(property);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、 " + name + "知识产权 更新成功");
                        }
                        else
                        {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、 " + name + "知识产权 已存在");
                        }
                	}else {
                		failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、 " + name + "知识产权 名称不能为空");
                	}
            	}else {
            		failureNum++;
            		String compName="";
            		if(StringUtils.isNotEmpty(property.getCompName())) {
            			compName+=property.getCompName();
            		}
                    failureMsg.append("<br/>" + failureNum + "、公司" + compName + " 不存在");
            	}
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、知识产权 " + name  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
    /**
     * 查询知识产权
     * 
     * @param iproId 知识产权ID
     * @return 知识产权
     */
    @Override
    public StbIntellectualProperty selectStbIntellectualPropertyById(Long iproId)
    {
        return stbIntellectualPropertyMapper.selectStbIntellectualPropertyById(iproId);
    }

    /**
     * 查询知识产权列表
     * 
     * @param stbIntellectualProperty 知识产权
     * @return 知识产权
     */
    @Override
    public List<StbIntellectualProperty> selectStbIntellectualPropertyList(StbIntellectualProperty stbIntellectualProperty)
    {
        return stbIntellectualPropertyMapper.selectStbIntellectualPropertyList(stbIntellectualProperty);
    }

    /**
     * 新增知识产权
     * 
     * @param stbIntellectualProperty 知识产权
     * @return 结果
     */
    @Override
    public int insertStbIntellectualProperty(StbIntellectualProperty stbIntellectualProperty)
    {
        stbIntellectualProperty.setCreateTime(DateUtils.getNowDate());
        return stbIntellectualPropertyMapper.insertStbIntellectualProperty(stbIntellectualProperty);
    }

    /**
     * 修改知识产权
     * 
     * @param stbIntellectualProperty 知识产权
     * @return 结果
     */
    @Override
    public int updateStbIntellectualProperty(StbIntellectualProperty stbIntellectualProperty)
    {
        stbIntellectualProperty.setUpdateTime(DateUtils.getNowDate());
        return stbIntellectualPropertyMapper.updateStbIntellectualProperty(stbIntellectualProperty);
    }

    /**
     * 删除知识产权对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStbIntellectualPropertyByIds(String ids)
    {
        return stbIntellectualPropertyMapper.deleteStbIntellectualPropertyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除知识产权信息
     * 
     * @param iproId 知识产权ID
     * @return 结果
     */
    @Override
    public int deleteStbIntellectualPropertyById(Long iproId)
    {
        return stbIntellectualPropertyMapper.deleteStbIntellectualPropertyById(iproId);
    }
    
	/*
	 * 查询各类知识产权
	 */


	@Override
	public List<Map<String, Object>> selectintellectualpropertytypeCount(StbIntellectualProperty stbIntellectualProperty) {

		return stbIntellectualPropertyMapper.selectintellectualpropertytypeCount(stbIntellectualProperty);
	}

	@Override
	public List<Map<String, Object>> selectintellectualpropertyCount(StbIntellectualProperty stbIntellectualProperty) {
		return stbIntellectualPropertyMapper.selectintellectualpropertyCount(stbIntellectualProperty);
	}


}
